#include "caskstopcontrol.h"

CaskStopControl::CaskStopControl(QWidget *parent)
    : QWidget{parent}
{}

CaskStopControl::CaskStopControl(const QString &jointName, const QJsonObject &functionDetails, QWidget *parent)
{
    QGroupBox *groupBox = new QGroupBox(jointName, this);
    groupBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    createCaskStopControl(groupBox, functionDetails);

    // 创建 QVBoxLayout 并设置到当前 QWidget
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(groupBox); // 将 groupBox 添加到布局
    layout->setContentsMargins(0, 0, 0, 0); // 可选，去除边距
    qDebug() << "CaskStopControl";

    setLayout(layout);

    //创建fastdds节点
    // CNP_DOMAIN_ID domain = 0;
    // std::string serverAppID = "ORS_Cask";
    // mCaskStopNode = new CNPClient(domain, serverAppID);
}


CaskStopControl::~CaskStopControl()
{
    //delete mCaskStopNode;
}

void CaskStopControl::createCaskStopControl(QGroupBox *groupBox, const QJsonObject &functionDetails)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(groupBox);  // 创建主布局

    // 创建一个 QWidget 来放置所有按钮
    QWidget *buttonWidget = new QWidget(groupBox);
    QHBoxLayout *buttonLayout = new QHBoxLayout(buttonWidget);  // 水平布局

    // 左侧弹簧
    QSpacerItem *leftSpacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);
    buttonLayout->addItem(leftSpacer);

    // 解析 JSON 中的每个控件
    for (auto it = functionDetails.begin(); it != functionDetails.end(); ++it)
    {
        QString key = it.key();  // "stopHinge" 或 "stopShuttle"
        QJsonObject controlDetails = it.value().toObject();

        // 创建 QPushButton
        QPushButton *button = new QPushButton(controlDetails["pushbutton"].toString(), buttonWidget);
        button->setObjectName("btn_" + key);  // 设置按钮的 objectName
        button->setMinimumWidth(100);
        button->setMinimumHeight(40);

        QFont buttonFont = button->font();
        buttonFont.setPointSize(12);  // 设置较大字体
        button->setFont(buttonFont);

        QString buttonText = controlDetails["pushbutton"].toString();
        if (key == "lock") {
            buttonText = "Lock";  // 默认文本为“关闭”
        }

        button->setText(buttonText);
        buttonLayout->addWidget(button);

        // 添加弹簧：控件之间的间隔
        QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding);
        buttonLayout->addItem(spacer);

        // 连接按钮点击信号到相应的槽函数
        if (key == "stopHinge") {
            connect(button, &QPushButton::clicked, this, &CaskStopControl::onStopHingeClicked);
        } else if (key == "stopShuttle") {
            connect(button, &QPushButton::clicked, this, &CaskStopControl::onStopShuttleClicked);
        } else if (key == "lock") {
            connect(button, &QPushButton::clicked, this, &CaskStopControl::onLockClicked);  // 处理 "lock" 按钮
        } else {
            qWarning() << "未处理的按钮名称:" << key;
        }
    }

    // 右侧弹簧
    QSpacerItem *rightSpacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);
    buttonLayout->addItem(rightSpacer);

    // 将按钮容器添加到主布局
    mainLayout->addWidget(buttonWidget);
}

// 槽函数：处理 stopHinge 按钮点击
void CaskStopControl::onStopHingeClicked()
{
    qDebug() << "stopHinge 按钮点击!";
    // 执行停止 Hinge 的具体操作...
    QString content = "stopHinge()";
    pubCaskStop(content);
}

// 槽函数：处理 stopShuttle 按钮点击
void CaskStopControl::onStopShuttleClicked()
{
    qDebug() << "stopShuttle 按钮点击!";
    // 执行停止 Shuttle 的具体操作...//stopShuttle()
    QString content = "stopShuttle()";
    pubCaskStop(content);
}

void CaskStopControl::onLockClicked()
{
    QString content;
    QPushButton *button = findChild<QPushButton*>("btn_lock");
    if (button) {
        QString currentText = button->text();
        if (currentText == "Lock") {
            button->setText("Open");
            qDebug() << "Lock 按钮点击! 状态：Open";
            // 执行Open逻辑...//lock(s=True)
            content = "lock(s=True)";
            pubCaskStop(content);
        } else {
            button->setText("Lock");
            qDebug() << "Open 按钮点击! 状态：Lock";
            // 执行关闭逻辑...
            content = "lock(s=False)";
            pubCaskStop(content);
        }
    } else {
        qWarning() << "未找到 btn_lock";
    }
}

void CaskStopControl::pubCaskStop(QString content)
{

    //CnpMsg mCaskStopMsg;
    mCaskStopMsg.device_id("ORS_Cask");
    auto now = std::chrono::system_clock::now();
    auto timestamp_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                            now.time_since_epoch()
                            ).count();
    mCaskStopMsg.device_id(std::to_string(timestamp_ms));
    mCaskStopMsg.content(content.toStdString());
    if (!mCaskStopNode)
        std::cout << "mCaskStopNode is dead" << std::endl;
    else
    {
        CNP_RESULT result = mCaskStopNode->publishCmdRequest(mCaskStopMsg);
        if (result == CNP_RESULT::CNP_PUBLISH_SUCCESS) {
            std::cout << "Message " << mCaskStopMsg.content() << " published successfully." << std::endl;
        }
        else {
            std::cout << "Failed to publish message " << "." << std::endl;
        }
    }
}

